<?php

namespace App\Jobs;

use App\Constant\NewFlowEnum;
use App\Events\LoanRequestRetryEvent;
use App\Events\NewFlowOrderEvent;
use App\Events\UserLoanApplyEvent;
use App\Facades\MoneyPlatformRequestFacade;
use App\Models\LoanApply;
use App\Models\SettingRepayRate;
use App\Models\UserLoanRequest;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class GetLoanResultJobQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;


    protected $loanObj;

    public function __construct(LoanApply $apply)
    {
        $this->loanObj = $apply;
    }

    /**
     * Description: 处理放款状态为放款中的数据
     * Author: liuju
     * DateTime: 2019-01-15 10:52
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Throwable
     */
    public function handle()
    {
        try {
            $loan = LoanApply::find($this->loanObj->id);
            if ($loan && $loan->loan_status == LoanApply::LOAN_STATUS_PROCESSING || $loan->loan_status == LoanApply::LOAN_RETRY_APPLY) {
                $res = MoneyPlatformRequestFacade::queryLoanResult($loan->loan_id);
                Log::debug('「loan_request_id为('.$loan->loan_request_id.')的放款处理结果：」'.json_encode($res,JSON_UNESCAPED_UNICODE));
                event(new NewFlowOrderEvent(NewFlowEnum::NewFlowLoanApply, $loan, 0, $res));
                if ($res['code'] === '01') {
                    $data = $res['data'];
                    if($res['subCode']){
                        $loan->sub_code = $res['subCode'];
                    }
                    if(!empty($res['message'])){
                        $loan->message =  $res['message'];
                    }
                    if(!empty($res['sign'])){
                        $loan->loan_sign = $res['sign'];
                    }
                    if(!empty($data['transMoneyList'])){
                        $loan->trans_money_list = $data['transMoneyList'];
                    }
                    $loan->loan_status = LoanApply::getLoanStatusByRes($res);
                    if($res['subCode'] == "F"){
                        $message = $res['message'];
                        if(mb_strstr($message, "金额") !== false || mb_strstr($message, "超限") !== false || mb_strstr($message, "限额") !== false ) {
                            $loan->loan_status = LoanApply::LOAN_RETRY_APPLY;

                        }
                    }

                    if ($loan->loan_status == LoanApply::LOAN_STATUS_SUCCESS) {
                        $loan->loan_id = $data['loanId'];
                        $loan->loan_time = $data['loanTime'];
                        $getDetail = LoanApply::getDetailByTransMoneyList($data['transMoneyList']);
                        if ($getDetail['arrival_account']) {
                            $loan->arrival_account = $getDetail['arrival_account'];
                        }
                        if ($getDetail['pre_insurance_amt']) {
                            $loan->pre_insurance_amt = $getDetail['pre_insurance_amt'];
                        }
                        if($getDetail['performance_insurance']){
                            $loan->performance_insurance = $getDetail['performance_insurance'];
                        }
                    }
                    $loan->saveOrFail();
                    event(new UserLoanApplyEvent($loan));
                }
            }
        } catch (\Exception $e) {
            Log::warning('轮询放款结果时由于文件' . $e->getFile() . '的' . $e->getLine() . '行中' . $e->getMessage() . '导致此次队列执行失败' . PHP_EOL);
        }

    }



}
